<template>
  <div class='appoint-box first-box'>
    <Form :formData="formData" :formParam="formParam"></Form>
    <Buttons class="margintop80" :buttonData="buttonData" ></Buttons>
  </div>
</template>
<script>
import Form from '../../common/form'
import Buttons from '../../common/button.vue'
export default {
  components: {Form,Buttons},
  name: "",
  data() {
    return {
      formData:[
        {
          title:{
            show:false,
          },
          children:[
            {type:"select",label:"预约类型",key:"booking_set_id",array:[],select:this.selectType},
            {type:"date",label:"到店日期",key:"arrival_time1",},
            {type:"select",label:"到店时间",key:"arrival_time2", array:[]},
            {type:"select",label:"服务顾问",key:"serviceId", array:[]}
          ],
        },{
          title:{
            show:false,
          },
          children:[
            {type:"input",label:"车型号",key:"carModel"},
            {type:"input",label:"车牌号",key:"plateNum"},
            {type:"input",label:"当前里程数",key:"currentMileage",unit:"公里"},
            {type:"input",label:"姓名",key:"realName"},
            {type:"input",label:"手机号",key:"phone"},
          ]
        },{
          title:{
            show:false,
          },
          children:[
            {type:"input",label:"备注",key:"remark"},
          ]
        }
        
      ],
      formParam:{
        realName:"",
        phone:"",
        carModel:"",
        plateNum:"",
        currentMileage:"",//
        lastMaintainKm:"",
        serviceId:"",//顾问id
        booking_set_id:"",
        arrival_time1:"",
        arrival_time2:"",
        car_id:"",//carid
        remark:"",

      },

      buttonData:[
        {
          label:"咨询",
          click:this.consolte,
        },{
          label:"确定",
          click:this.save,
        },
      ],

      timesArray:[],
    };
  },
  methods: {
    save(){
      var params={
        booking_set_id:this.formParam.booking_set_id,
        car_id:this.formParam.car_id,
        remark:this.formParam.remark,
        arrivalDate:this.formParam.arrival_time1,
        arrivalTimes:this.formParam.arrival_time2,
        serviceId:this.formParam.serviceId,
        carModel:this.formParam.carModel,
        plateNum:this.formParam.plateNum,
        currentMileage:this.formParam.currentMileage,
      }
      if(!params.booking_set_id){
        _store.state.dialogs.feedback.type="cancel"
        _store.state.dialogs.feedback.content="预约类型不能为空"
        _store.state.dialogs.feedback.show=true
        return
      }else if(!params.arrivalTimes){
        _store.state.dialogs.feedback.type="cancel"
        _store.state.dialogs.feedback.content="到店时间不能为空"
        _store.state.dialogs.feedback.show=true
        return
      }
  
      _g.apiPost("booking/insert.do",params).then(res=>{
        _g.toMessage(res)
        if(!res.error){
          setTimeout(()=>{
            this.$router.go(-1)
          },800)
        }
      })
    },
    
    selectType(item){
      var goal=[]
      this.$set(this.formData[0].children[2],"array",[])
      this.timesArray.forEach(ele=>{
        if(ele.type==this.formParam[item.key]){
          goal.push(ele.arrivalTimes)
        }
      })
      this.$set(this.formData[0].children[2],"array",goal)
      if(goal.length>0){
        this.formParam.arrival_time2=goal[0]
      }
    },

    consolte(){
      this.$router.push("/advisers")
    },

    // 获取信息
    getInfo(){
      _g.apiPost("booking/init.do",null,true).then(res=>{
        if(res.msg=="请编辑车辆信息"){
          this.$store.state.dialogs.alert.content=res.msg
          this.$store.state.dialogs.alert.show=true
          return
        }
        this.formParam.realName=res.data.user.realName||""
        this.formParam.phone=res.data.user.phone||""
        this.formParam.plateNum=res.data.vehicle.plateNum||""
        this.formParam.carModel=res.data.vehicle.carModel||""
        this.formParam.currentMileage=res.data.vehicle.currentMileage||0
        this.formParam.car_id=res.data.vehicle.id||""
        this.formParam.name=res.data.vehicle.name||""
        this.formParam.serviceId=res.data.user.serviceId

        this.timesArray=res.data.list
        // 保存用户的id
        sessionStorage.setItem("userId",res.data.user.id||1)
        sessionStorage.setItem("userName",res.data.user.realName||"张三")
        sessionStorage.setItem("userPhone",res.data.user.phone||"12345678911")

        var advisersA=[]
        res.data.Employees.forEach(ele => {
            advisersA.push({
              key:ele.id,
              value:ele.realName
            })
        });
        this.$set(this.formData[0].children[3],"array",advisersA)

        // 设置预约类型
        var typeArray = []
        for(var key in res.data.map){
          typeArray.push({
            key,
            value:res.data.map[key]
          })
        }
        this.$set(this.formData[0].children[0],"array",typeArray)
        
      })
    }
  },
  created() {
    this.getInfo()
  }
};
</script>
<style lang="scss" scoped>
.appoint-box{
  
}
.margintop80{
  padding: 80px 15px 0;
}
</style>